package com.lemon.jvm.hotspot.src.share.vm.oops;

import java.util.HashMap;
import java.util.Map;

public class ConstantPool {

    public static final int JVM_CONSTANT_Utf8 = 1;
    public static final int JVM_CONSTANT_Unicode = 2;   /* unused */
    public static final int JVM_CONSTANT_Integer = 3;
    public static final int JVM_CONSTANT_Float = 4;
    public static final int JVM_CONSTANT_Long = 5;
    public static final int JVM_CONSTANT_Double = 6;
    public static final int JVM_CONSTANT_Class = 7;
    public static final int JVM_CONSTANT_String = 8;
    public static final int JVM_CONSTANT_Fieldref = 9;
    public static final int JVM_CONSTANT_Methodref = 10;
    public static final int JVM_CONSTANT_InterfaceMethodref = 11;
    public static final int JVM_CONSTANT_NameAndType = 12;

    private int length;

    //index => type
    private int[] tag;

    //index => value;
    private Map<Integer,Object> dataMap;

    public ConstantPool(int size) {
        this.length = size;
        this.tag = new int[size];
        this.dataMap = new HashMap<>(size);
    }

    public void add(int index,int tag,Object v){
        this.tag[index] = tag;
        dataMap.put(index,v);
    }

    public String getNameByNameIndex(int nameIndex){
        return (String)dataMap.get(nameIndex);
    }

    public String getMethodNameByMethodInfo(int operand){
        int i = (int)dataMap.get(operand);
        return (String)dataMap.get(i);
    }

    public int getLength() {
        return length;
    }


}
